//
//  NSString+HFSafe.m
//  HeFanLive
//
//  Created by Shelin on 16/11/18.
//  Copyright © 2016年 StarUnion. All rights reserved.
//

#import "NSString+HFSafe.h"
#import "NSObject+HFSafe.h"
@implementation NSString (HFSafe)

#pragma mark - Method Swizzling

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool {
            [objc_getClass("__NSCFConstantString") swizzleMethod:@selector(substringFromIndex:) withMethod:@selector(safeSubstringFromIndex:) error:nil];
            
            [objc_getClass("__NSCFConstantString") swizzleMethod:@selector(substringToIndex:) withMethod:@selector(safeSubstringToIndex:) error:nil];
            
            [objc_getClass("__NSCFConstantString") swizzleMethod:@selector(substringWithRange:) withMethod:@selector(safeSubstringWithRange:) error:nil];
        };
    });
}

- (NSString *)safeSubstringFromIndex:(NSInteger)from {
    NSInteger length = self.length;
    
    if (from > length || from < 0) {
        NSLog(@"substringFromIndex - 字符串越界！");
        return @"";
    }
    return [self safeSubstringFromIndex:from];
}

- (NSString *)safeSubstringToIndex:(NSInteger)to {
    NSInteger length = self.length;
    
    if (to > length || to < 0) {
        NSLog(@"substringToIndex - 字符串越界！");
        return @"";
    }
    return [self safeSubstringToIndex:to];
}

- (NSString *)safeSubstringWithRange:(NSRange)range {
    NSInteger length = self.length;
    if ((range.location + range.length) > length) {
        NSLog(@"substringWithRange - 字符串越界！");
        return @"";
    }
    return [self safeSubstringWithRange:range];
}

@end

 

